{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import yfinance as yf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# update stock list in this file and run the code\n",
    "tickers = pd.read_excel(\"stock_list.xlsx\")[\"Ticker\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[*********************100%***********************]  1 of 1 completed\n",
      "MSFT data starting from 1986-03-13 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "V data starting from 2008-03-19 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "MA data starting from 2006-05-25 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "CSCO data starting from 1990-02-16 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "AAPL data starting from 1980-12-12 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "INTC data starting from 1980-03-17 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "JNJ data starting from 1962-01-02 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "UNH data starting from 1984-10-17 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "MRK data starting from 1970-01-02 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "PFE data starting from 1972-06-01 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "JPM data starting from 1980-03-17 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "BRK-B data starting from 1996-05-09 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "BAC data starting from 1973-02-21 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "WFC data starting from 1972-06-01 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "FB data starting from 2012-05-18 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "VZ data starting from 1983-11-21 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "GOOG data starting from 2004-08-19 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "T data starting from 1983-11-21 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "NFLX data starting from 2002-05-23 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "AMZN data starting from 1997-05-15 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "HD data starting from 1981-09-22 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "MCD data starting from 1966-07-05 00:00:00\n",
      "[*********************100%***********************]  1 of 1 completed\n",
      "NKE data starting from 1980-12-02 00:00:00\n"
     ]
    }
   ],
   "source": [
    "returns_df = pd.DataFrame()\n",
    "for i in tickers:\n",
    "    data = yf.download(i, period=\"max\")\n",
    "    data.to_csv(f\"Data\\\\{i}.csv\")\n",
    "    # data[f\"{i}\"] = data[\"Adj Close\"].pct_change(periods=1, fill_method='ffill') # arithmetic returns\n",
    "    data[f\"{i}\"] = data[\"Adj Close\"].pct_change(periods=1, fill_method='ffill')\n",
    "    returns_df = returns_df.merge(data[f\"{i}\"], how=\"outer\", right_index=True, left_index=True)    \n",
    "    print(f\"{i} data starting from {data.index[0]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "returns_df.to_csv(\"Returns.csv\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
